package com.peopledelete.controller;

import com.peopledelete.service.OrigindayrecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author cyt
 * @create 2020-04-26 23:42
 */

@RestController
public class peopleController {


    @Autowired
    OrigindayrecordsService service;

    /**
     * 向前端发送数据， 刷新控制台的界面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPeopleNumber")
    public HashMap getPeopleNumber(HttpServletRequest request) {
        ServletContext servletContext = request.getSession().getServletContext();
        String inNum = (String) servletContext.getAttribute("inNum");
        String outNum = (String) servletContext.getAttribute("outNum");
        HashMap<Object, Object> map = new HashMap<>();
        map.put("inNum", inNum);
        map.put("outNum", outNum);
        return map;
    }

    /**
     * 从 py 得到当前的数值
     *
     * @param peopleNum
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/people")
    public String peopleCount(@RequestParam("inNum") String inNum, @RequestParam("outNum") String outNum, HttpServletRequest request) {
        ServletContext context = request.getSession().getServletContext();
        Integer subFlag = (Integer) request.getSession().getAttribute("sub_flag");
        if (subFlag == null) {
            request.getSession().setAttribute("sub_flag", 1);
        }
        request.getSession().getServletContext().setAttribute("inNum", inNum);
        request.getSession().getServletContext().setAttribute("outNum", outNum);
        System.out.println("Python端发送的出站流数量是：" + outNum);
        System.out.println("Python端发送的入站流数量是：" + inNum);
        service.insertDate(Integer.parseInt(inNum), Integer.parseInt(outNum));
        return "index";
    }

    // 查询出当天的累计出站、入站人数 ， 在控制台显示
    @RequestMapping("/CntNum")
    public HashMap CntNum() {
        List<Map<String, Object>> cntNum = service.getCntNum();
        HashMap<Object, Object> cntMap = new HashMap<>();
        for (Map<String, Object> map : cntNum) {
            int CntOutNum = ((BigDecimal) map.get("OutNum")).intValue();
            int CntInNum = ((BigDecimal) map.get("InNum")).intValue();
            System.out.println(CntOutNum);
            System.out.println(CntInNum);
            cntMap.put("CntOutNum", CntOutNum);
            cntMap.put("CntInNum", CntInNum);
        }
        return cntMap;
    }
}


